/*************************
日常考核的指标库弹窗
* */
<template>
  <div class="mod-config">
    <basic-container>
      <div class="avue-crud">
        <el-radio-group v-model="tabType" @change="getDataList" style="margin-bottom:10px;">
          <el-radio-button label="1">通用指标</el-radio-button>
          <el-radio-button label="2">个性指标</el-radio-button>
        </el-radio-group>
        <div class="pull-right">
          <el-button type="primary" @click="addZhibiaoku()">新增指标库</el-button>
        </div>
        <el-table
          :data="dataList"
          stripe
          tooltip-effect="light"
          ref="tableRef"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" header-align="center" align="center"  fixed="left"></el-table-column>
          <el-table-column prop="normLx" label="指标类型" min-width="90" fixed="left" show-overflow-tooltip></el-table-column>
          <el-table-column prop="normName" label="指标名称" min-width="150" show-overflow-tooltip></el-table-column>
          <el-table-column prop="normDw" label="指标单位" min-width="90" show-overflow-tooltip></el-table-column>
          <el-table-column prop="normGsname" label="计算公式" show-overflow-tooltip min-width="150"></el-table-column>
          <el-table-column prop="normSm" show-overflow-tooltip label="指标说明" min-width="200"></el-table-column>
          <el-table-column prop="normPx" label="加扣范围" min-width="100" v-if="tabType==3">
            <template slot-scope="scope">
              <span>{{ scope.row.normjka }}-{{ scope.row.normjkb }}</span>
              <span></span>
            </template>
          </el-table-column>
          <el-table-column prop="normPx" label="指标序号" min-width="100"></el-table-column>
          <el-table-column prop="normRule" label="指标规则" min-width="100"></el-table-column>
          <el-table-column prop="normGetDate" label="数据获取方式" min-width="120"></el-table-column>
          <el-table-column label="操作" align="center" min-width="220">
            <template slot-scope="scope">
              <el-button type="primary" plain @click="copyHandle(scope.row.normId)">复制</el-button>
              <el-button type="primary" @click="editZhiBiao(scope.row)" plain>编辑</el-button>
              <el-button type="primary" @click="delZhiBiao(scope.row,scope.$index)" plain>删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
      </div>
    </basic-container>
  </div>
</template>
<script>
import { getNormByTypeAndDeptId } from "@/api/hetongrenwu/hetongyeji/hetongzb";
import { fetchList } from "@/api/admin/sysnorm";

import { delObj, copyHandle, pushHandle } from "@/api/admin/sysnorm";
import { mapGetters } from "vuex";
import TableForm from "@/views/dailyExamine/components/sysnorm-form";
export default {
  data() {
    return {
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      tabType: "2",
      text: "",
      addOrUpdateVisible: true,
      enter: -1,
      multipleSelection: [],
      formName: ""
    };
  },
  props: {
    hetongId: Number
  },
  computed: {
    ...mapGetters({
      userInfo: "userInfo"
    })
  },
  components: {
    TableForm
  },
  methods: {
    copyHandle(id) {
      this.$confirm("是否复制这条指标？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return copyHandle(id);
        })
        .then(data => {
          this.$message.success("复制成功");
          this.getDataList();
        });
    },
    pushHandle(id) {
      this.$confirm("是否推送这条指标？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return pushHandle(id);
        })
        .then(data => {
          this.$message.success("推送成功");
          this.getDataList();
        });
    },
    //从合同创建进入时
    hetongEnter(enter, formName) {
      this.enter = enter;
      this.formName = formName;
      this.getDataList();
    },
    handleSelectionChange(val) {
      this.$emit("func", val);
    },
    init(fname, tabtypes) {
      this.formName = fname;
      this.tabType = tabtypes;
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      this.addOrUpdateVisible = false;
      if (this.tabType == "1") {
        fetchList({
          current: 1,
          size: 1000,
          dataState: 1,
          normType: "rckh_zb"
        }).then(response => {
          this.dataList = response.data.data.records.filter(it => {
            return it.normFl == "ty";
          });
        });
      } else {
        fetchList({
          current: 1,
          size: 1000,
          dataState: 1,
          normType: "rckh_zb"
        }).then(response => {
          this.dataList = response.data.data.records.filter(it => {
            return it.normFl == "gx";
          });
        });
      }
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    saveZhibiao(e) {
      const rows = this.$refs.tableRef.selection;
      if (rows.length < 1) {
        this.$message.closeAll();
        this.$message({
          type: "info",
          message: "请选择指标添加"
        });
      } else {
        this.duibiArr(e, rows);
        // debugger;
        if (this.text) {
          this.$confirm("指标名称：" + this.text + "重复", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
            .then(function() {
              this.$emit("func", rows);
            })
            .catch(data => {});
        } else {
          this.$emit("func", rows);
        }
      }
    },
    // 对比重复
    duibiArr(oldArr, selectArr) {
      //console.log(selectArr)
      selectArr.forEach(item => {
        var sameObj = oldArr.find(
          oldItem => oldItem.zhibiaoName == item.normName
        );
        //console.log(sameObj)
        if (sameObj) {
          this.text = this.text + sameObj.zhibiaoName + "、";
        } else {
          this.text = "";
        }
      });
    },
    addZhibiaoku(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, "rckh_zb", "rckh_zb", this.tabType);
      });
    },
    //编辑指标库
    editZhiBiao(row) {
      //console.log(row)
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(
          row.normId,
          "rckh_zb",
          "rckh_zb",
          this.tabType
        );
      });
    },
    //删除指标库
    delZhiBiao(row, index) {
      this.$confirm("是否确认删除" + row.normName + "指标", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delObj(row.normId);
        })
        .then(data => {
          this.$message.closeAll();
          this.$message.success("删除成功");
          this.getDataList();
        });
    }
  }
};
</script>
